﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dialog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            //点击弹出对话框
            var dialog = new OpenFileDialog();

            //设置对话框属性
            dialog.Title = "请选择要打开的文件"; //标题
            dialog.Multiselect = true;  //支持对话框可以多选
            dialog.InitialDirectory = @"G:\Gitee\CsharpAdvanced\Dialog\"; //默认目录
            dialog.Filter = "文本文件|*.txt";

            //展开对话框
            dialog.ShowDialog();

            var path = dialog.FileNames;
            if (path.Length == 0)
            {
                MessageBox.Show("请选择一个文件！");
                return;
            }
            else
            {
                //读取文件的内容到文本框
                txtFile.Text = "";
                foreach (var item in path)
                { 
                    string str;
                    using(FileStream fsRead = new FileStream(item, FileMode.OpenOrCreate, FileAccess.Read))
                    {
                        byte[] buffer = new byte[1024 * 1024 * 5];
                        int r = fsRead.Read(buffer, 0, buffer.Length);

                        //将读出的二进制转换成utf-8格式的字符串
                         str= Encoding.UTF8.GetString(buffer, 0, r)+"\r\n";
                    }
                    txtFile.Text += str;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //点击弹出对话框
            var dialog = new SaveFileDialog();

            //设置对话框属性
            dialog.Title = "请选择要保存的路径"; //标题
            dialog.InitialDirectory = @"G:\Gitee\CsharpAdvanced\Dialog\"; //默认目录
            dialog.Filter = "文本文件|*.txt";
            
            //展开对话框
            dialog.ShowDialog();

            //保存文本框内容
            var path = dialog.FileName;
            if (path != "")
            {
                using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(txtFile.Text);
                    fsWrite.Write(buffer, 0, buffer.Length);
                    MessageBox.Show("保存成功");
                }
            }
        }

        private void btnFont_Click(object sender, EventArgs e)
        {
            //选择字体
            var fd = new FontDialog();
            fd.ShowDialog();

            //把选好的字体赋值
            txtFile.Font = fd.Font;
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            //选择颜色
            var cd = new ColorDialog();
            cd.ShowDialog();

            //把选好的颜色赋值
            txtFile.ForeColor = cd.Color; //字体颜色
            //txtFile.BackColor = cd.Color; //背景颜色
        }
    }
}
